﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Common
{
    public class StringHelper
    {
        public static string ClearHtml(string stringVal)
        {
            if (stringVal.Length > 0)
            {
                //删除脚本
                stringVal = Regex.Replace(stringVal, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
                //删除HTML
                stringVal = Regex.Replace(stringVal, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
                stringVal = Regex.Replace(stringVal, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
                stringVal = Regex.Replace(stringVal, @"-->", "", RegexOptions.IgnoreCase);
                stringVal = Regex.Replace(stringVal, @"<!--.*", "", RegexOptions.IgnoreCase);
                stringVal = Regex.Replace(stringVal, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
                stringVal = Regex.Replace(stringVal, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
                stringVal = Regex.Replace(stringVal, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
                stringVal = Regex.Replace(stringVal, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
                stringVal = Regex.Replace(stringVal, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
                stringVal = Regex.Replace(stringVal, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
                stringVal = Regex.Replace(stringVal, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
                stringVal = Regex.Replace(stringVal, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
                stringVal = Regex.Replace(stringVal, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
                stringVal = Regex.Replace(stringVal, @"&#(\d+);", "", RegexOptions.IgnoreCase);
                stringVal = Regex.Replace(stringVal, @"&ldquo;", "\"", RegexOptions.IgnoreCase);//保留【 “ 】的标点符合
                stringVal = Regex.Replace(stringVal, @"&rdquo;", "\"", RegexOptions.IgnoreCase);//保留【 ” 】的标点符合
                stringVal.Replace("<", "");
                stringVal.Replace(">", "");
                stringVal.Replace("\r\n", "");
                stringVal = HttpContext.Current.Server.HtmlEncode(stringVal).Trim();
            }
            return stringVal;
        }
    }
}
